package sopt.dev.data;

import java.util.List;

import sopt.dev.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CityDetailAdapter extends BaseExpandableListAdapter {
	private List<String> groupList; 
	private List<List<ItemVO>> childList;
	private LayoutInflater inflater = null;
	private ViewHolder viewHolder = null;

	public CityDetailAdapter(Context c, List<String> mGroupList,
			List<List<ItemVO>> mChildList) {
		super();
		this.inflater = LayoutInflater.from(c);
		this.groupList = mGroupList;
		this.childList = mChildList;
	}

	// 그룹 포지션을 반환한다.
	@Override
	public String getGroup(int groupPosition) {
		return groupList.get(groupPosition);
	}

	// 그룹 사이즈를 반환한다.
	@Override
	public int getGroupCount() {
		return groupList.size();
	}

	// 그룹 ID를 반환한다.
	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	// 그룹뷰 각각의 ROW
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		View v = convertView;

		if (v == null) {
			viewHolder = new ViewHolder();
			v = inflater.inflate(R.layout.city_detail_item_group, parent, false);
			viewHolder.tv_groupName = (TextView) v.findViewById(R.id.city_detail_item_tv_indexdate);
			v.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) v.getTag();
		}

		viewHolder.tv_groupName.setText(getGroup(groupPosition));

		return v;
	}

	// 차일드뷰를 반환한다.
	@Override
	public ItemVO getChild(int groupPosition, int childPosition) {
		return childList.get(groupPosition).get(childPosition);
	}

	// 차일드뷰 사이즈를 반환한다.
	@Override
	public int getChildrenCount(int groupPosition) {
		return childList.get(groupPosition).size();
	}

	// 차일드뷰 ID를 반환한다.
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	// 차일드뷰 각각의 ROW
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		View v = convertView;

		if (v == null) {
			viewHolder = new ViewHolder();
			v = inflater.inflate(R.layout.city_detail_item, parent, false);
			viewHolder.iv_itemPhoto = (ImageView) v.findViewById(R.id.city_detail_item_iv_item);
			viewHolder.iv_icon = (ImageView) v.findViewById(R.id.city_detail_item_iv_icon);
			viewHolder.tv_childName = (TextView) v.findViewById(R.id.city_detail_item_tv_name);
			viewHolder.tv_echange = (TextView) v.findViewById(R.id.city_detail_item_tv_exchange);
			viewHolder.tv_won = (TextView) v.findViewById(R.id.city_detail_item_tv_won);
			
			v.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) v.getTag();
		}

		ItemVO item = getChild(groupPosition, childPosition);
		
		viewHolder.tv_childName.setText(item.getName());
		viewHolder.tv_won.setText(String.valueOf(item.getPrice()));
		// TODO ui 표시 안되있음
		
		return v;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}
	
	public void removeGroup(int groupPosition) {
		
		childList.get(groupPosition).clear();;
		groupList.remove(groupPosition);
		notifyDataSetChanged();
	}
	
	public void removeChild(int groupPositoin, int childPosition) {
		
		List<ItemVO> itemList = childList.get(groupPositoin);
		itemList.remove(childPosition);
		
		if(itemList.size() == 0) {
			groupList.remove(groupPositoin);	
		}
		
		notifyDataSetChanged();
	}
	
	public void addChild(int groupPosition, ItemVO item) {
		childList.get(groupPosition).add(item);
		notifyDataSetChanged();
	}
	

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}


	class ViewHolder {
		public TextView tv_groupName;
		public ImageView iv_itemPhoto;
		public ImageView iv_icon;
		public TextView tv_childName;
		public TextView tv_echange;
		public TextView tv_won;
	}

	
}
